package com.kele.type;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.UnpooledByteBufAllocator;

public enum DataType implements DatatypeConverter{

    Boolean {
        @Override
        public String get(ByteBuf byteBuf, int index) {
            throw new RuntimeException("Not supported");
        }

        @Override
        public String write(String value) {
            if ("1".equals(value)){
                return "FF00";
            } else if ("0".equals(value)){
                return "0000";
            } else {
                throw new RuntimeException("Boolean类型只支持1或0");
            }
        }
    },

    Short {
        @Override
        public String get(ByteBuf byteBuf, int index) {
            return String.valueOf(byteBuf.getShort(index));
        }

        @Override
        public String write(String value) {
            short i = java.lang.Short.parseShort(value);
            ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer().writeShort(i);
            return ByteBufUtil.hexDump(byteBuf);
        }
    },

    ShortLE {
        @Override
        public String get(ByteBuf byteBuf, int index) {
            return String.valueOf(byteBuf.getShortLE(index));
        }

        @Override
        public String write(String value) {
            short i = java.lang.Short.parseShort(value);
            ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer().writeShortLE(i);
            return ByteBufUtil.hexDump(byteBuf);
        }
    },

    Int {
        @Override
        public String get(ByteBuf byteBuf, int index) {
            return String.valueOf(byteBuf.getInt(index));
        }

        @Override
        public String write(String value) {
            int i = Integer.parseInt(value);
            ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer().writeInt(i);
            return ByteBufUtil.hexDump(byteBuf);
        }
    },

    IntLE {
        @Override
        public String get(ByteBuf byteBuf, int index) {
            return String.valueOf(byteBuf.getIntLE(index));
        }

        @Override
        public String write(String value) {
            int i = Integer.parseInt(value);
            ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer().writeIntLE(i);
            return ByteBufUtil.hexDump(byteBuf);
        }
    },

    Float {
        @Override
        public String get(ByteBuf byteBuf, int index) {
            return String.valueOf(byteBuf.getFloat(index));
        }

        @Override
        public String write(String value) {
            float i = java.lang.Float.parseFloat(value);
            ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer().writeFloat(i);
            return ByteBufUtil.hexDump(byteBuf);
        }
    },

    FloatLE {
        @Override
        public String get(ByteBuf byteBuf, int index) {
            return String.valueOf(byteBuf.getFloatLE(index));
        }

        @Override
        public String write(String value) {
            float i = java.lang.Float.parseFloat(value);
            ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer().writeFloatLE(i);
            return ByteBufUtil.hexDump(byteBuf);
        }
    },

    Long {
        @Override
        public String get(ByteBuf byteBuf, int index) {
            return String.valueOf(byteBuf.getLong(index));
        }

        @Override
        public String write(String value) {
            long i = java.lang.Long.parseLong(value);
            ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer().writeLong(i);
            return ByteBufUtil.hexDump(byteBuf);
        }
    },

    LongLE {
        @Override
        public String get(ByteBuf byteBuf, int index) {
            return String.valueOf(byteBuf.getLongLE(index));
        }

        @Override
        public String write(String value) {
            long i = java.lang.Long.parseLong(value);
            ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer().writeLongLE(i);
            return ByteBufUtil.hexDump(byteBuf);
        }
    },

    Double {
        @Override
        public String get(ByteBuf byteBuf, int index) {
            return String.valueOf(byteBuf.getDouble(index));
        }

        @Override
        public String write(String value) {
            double i = java.lang.Double.parseDouble(value);
            ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer().writeDouble(i);
            return ByteBufUtil.hexDump(byteBuf);
        }
    },

    DoubleLE {
        @Override
        public String get(ByteBuf byteBuf, int index) {
            return String.valueOf(byteBuf.getDoubleLE(index));
        }

        @Override
        public String write(String value) {
            double i = java.lang.Double.parseDouble(value);
            ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer().writeDoubleLE(i);
            return ByteBufUtil.hexDump(byteBuf);
        }
    },

}
